package com.sunnay.bean;

import com.baomidou.mybatisplus.extension.incrementer.OracleKeyGenerator;
import com.gaby.commons.okhttp.OkHttpConfig;
import com.gaby.commons.okhttp.OkHttpUtils;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

public class BeanConfig {
    @Bean({"okHttpUtils"})
    public OkHttpUtils okHttpUtils()
    {

        OkHttpConfig okHttpConfig = new OkHttpConfig();
        okHttpConfig.setMaxIdleConnections(200);
        okHttpConfig.setReadTimeoutSecond(10);
        okHttpConfig.setConnectTimeoutSecond(10);

        OkHttpUtils okHttpUtils = new OkHttpUtils(false, okHttpConfig);

        okHttpUtils.getOkHttpClient().dispatcher().setMaxRequests(2000);
        okHttpUtils.getOkHttpClient().dispatcher().setMaxRequestsPerHost(2000);


        return okHttpUtils;
    }

    @Bean
    public OkHttpClient okHttpClient() {
        OkHttpClient.Builder okhttpClient = new OkHttpClient.Builder();
        okhttpClient.sslSocketFactory(sslSocketFactory(), x509TrustManager());
        okhttpClient.retryOnConnectionFailure(false);
        okhttpClient.connectionPool(pool());
        okhttpClient.connectTimeout(30, TimeUnit.SECONDS);
        okhttpClient.readTimeout(30, TimeUnit.SECONDS);
        okhttpClient.writeTimeout(30,TimeUnit.SECONDS);
        okhttpClient.hostnameVerifier((hostname, session) -> true);
        /*okhttpClient.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyProperties.getProxyServer(), proxyProperties.getProxyPort())));
        okhttpClient.proxyAuthenticator((route, response) -> {
            //设置代理服务器账号密码
            String credential = Credentials.basic(proxyProperties.getProxyUser(), proxyProperties.getProxyPass());
            System.out.println("初始化代理服务器");
            return response.request().newBuilder()
                    .header("Proxy-Authorization", credential)
                    .build();
        });*/
        return okhttpClient.build();
    }
    @Bean
    public X509TrustManager x509TrustManager() {
        return new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) {
            }
            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) {
            }
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        };
    }
    @Bean
    public SSLSocketFactory sslSocketFactory() {
        try {
            // 信任任何链接
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[]{x509TrustManager()}, new SecureRandom());
            return sslContext.getSocketFactory();
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Bean
    public ConnectionPool pool() {
        return new ConnectionPool(200, 300, TimeUnit.SECONDS);
    }

    @Bean
    public OracleKeyGenerator oracleKeyGenerator() {
        return new OracleKeyGenerator();
    }
}
